package com.duan.laugh.auth.config;

import com.duan.laugh.common.security.component.LaughUserAuthenticationConverter;
import com.duan.laugh.common.security.exception.LaughAccessDeniedHandler;
import com.duan.laugh.common.security.exception.LaughAuthExceptionEntryPoint;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter;

/**
 * 资源服务
 * check_token 的结果转化用户信息
 *
 * @author duanjw
 */
@Configuration
@EnableResourceServer
public class ResourceConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
        UserAuthenticationConverter userTokenConverter = new LaughUserAuthenticationConverter();
        accessTokenConverter.setUserTokenConverter(userTokenConverter);
        resources.authenticationEntryPoint(new LaughAuthExceptionEntryPoint()).accessDeniedHandler(new LaughAccessDeniedHandler());
    }
}
